﻿using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using ModernWpf;
using ModernWpf.Controls;

namespace YZAutoDWR;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
    private const double CollapseThreshold = 700; // MainWindow 低于这个宽度的时候折叠侧边栏
    private double _lastWidth = CollapseThreshold;

    public MainWindow()
    {
        InitializeComponent();
        ThemeManager.SetRequestedTheme(this, ElementTheme.Light); // 配置 ModernWPF 主题
        Loaded += (_, _) =>
        {
            _lastWidth = ActualWidth;
            UpdatePaneState(ActualWidth);
        };
        SizeChanged += MainWindow_SizeChanged;

        NavView.SelectionChanged += NavView_SelectionChanged;
        NavView.SelectedItem = NavView.MenuItems.OfType<NavigationViewItem>().First(); // 默认导航页面
    }

    private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
    {
        // 只有当窗口宽度变化超过1像素时才更新状态
        if (!(Math.Abs(e.NewSize.Width - _lastWidth) > 1)) return;
        _lastWidth = e.NewSize.Width;
        UpdatePaneState(e.NewSize.Width);
    }

    private void UpdatePaneState(double width)
    {
        NavView.IsPaneOpen = width >= CollapseThreshold;
    }

    private void NavView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args)
    {
        if (args.SelectedItem is NavigationViewItem item)
        {
            switch (item.Content.ToString())
            {
                case "日报生成":
                    ContentFrame.Navigate(new DailyWorkReport());
                    break;
            }
        }
    }

    protected override void OnClosing(CancelEventArgs e)
    {
        // 取消默认关闭行为
        e.Cancel = true;
        Hide();
        base.OnClosing(e);
    }
}